import api from "@/api/apiServer";
import { filterDate } from "@/unit/tool";
let processingOption = [];
let pickingOption = [];
async function getProcessingOption(parentCode) {
  let res = await api.DictFindByCode({ parentCode });
  processingOption = res.data;
}
async function getPickingOption(parentCode) {
  let res = await api.DictFindByCode({ parentCode });
  pickingOption = res.data;
}
getProcessingOption("dict.processing", processingOption);
getPickingOption("dict.picking", pickingOption);

// 格式化数据来源
function caseSource(value, option) {
  let result = option.filter((item) => {
    return item.id === value;
  });

  if (result.length > 0) {
    return result[0].dictName;
  } else {
    return "";
  }
}
// 获取不合格原因的数据
let badOptions = [];
async function getDictFindByCode(parentCode, key) {
  const res = await api.DictFindByCode({
    parentCode: "ReasonsForProducts",
  });
  badOptions = res.data;
}

getDictFindByCode();

function caseBadReson(id) {
  let arr = badOptions.filter((item) => (item.id = id));
  if (arr.length > 0) {
    return arr[0].dictName;
  } else {
    return "";
  }
}

const columns = [
  {
    title: "序号",
    dataIndex: "index",
    key: "index",
    customRender: (text, record, index) => `${index + 1}`,
    align: "center",
  },
  {
    title: "加工单号",
    dataIndex: "processNum",
    key: "processNum",
    align: "center",
  },

  {
    title: "生产计划",
    dataIndex: "planNum",
    key: "planNum",
    align: "center",
  },
  {
    title: "加工单数量",
    dataIndex: "productionQuantity",
    key: "productionQuantity",
    align: "center",
  },
  // {
  //   title: "完工数量",
  //   dataIndex: "rescri",
  //   key: "rescri",
  //   align: "center",
  // },
  {
    title: "班次",
    dataIndex: "workTeamName",
    key: "workTeamName",
    align: "center",
  },

  {
    title: "备料",
    dataIndex: "pickStatus",
    key: "pickStatus",
    align: "center",
    customRender: (text, record, index) => {
      return caseSource(text, pickingOption);
    },
  },
  {
    title: "状态",
    dataIndex: "status",
    key: "status",
    align: "center",
    customRender: (text, record, index) => {
      return caseSource(text, processingOption);
    },
  },
  {
    title: "开始时间",
    dataIndex: "processStartTime",
    key: "processStartTime",
    align: "center",
    customRender: (text, record, index) => {
      return filterDate(text);
    },
  },
  {
    title: "结束时间",
    dataIndex: "processEndTime",
    key: "processEndTime",
    align: "center",
    customRender: (text, record, index) => {
      return filterDate(text);
    },
  },
  {
    title: "备注",
    dataIndex: "remark",
    key: "remark",
    align: "center",
  },
  {
    width: 300,
    title: "操作",
    dataIndex: "action",
    align: "center",
    scopedSlots: { customRender: "action" },
  },
];

const orderColumns = [
  {
    title: "序号",
    dataIndex: "index",
    key: "index",
    customRender: (text, record, index) => `${index + 1}`,
    align: "center",
  },
  {
    title: "加工单号",
    dataIndex: "processNum",
    key: "processNum",
    align: "center",
  },

  {
    title: "生产计划",
    dataIndex: "planNum",
    key: "planNum",
    align: "center",
  },
  {
    title: "加工单数量",
    dataIndex: "productionQuantity",
    key: "productionQuantity",
    align: "center",
  },
  // {
  //   title: "完工数量",
  //   dataIndex: "rescri",
  //   key: "rescri",
  //   align: "center",
  // },
  {
    title: "班次",
    dataIndex: "workTeamName",
    key: "workTeamName",
    align: "center",
  },

  {
    title: "备料",
    dataIndex: "pickStatus",
    key: "pickStatus",
    align: "center",
    customRender: (text, record, index) => {
      return caseSource(text, pickingOption);
    },
  },
  {
    title: "状态",
    dataIndex: "status",
    key: "status",
    align: "center",
    customRender: (text, record, index) => {
      return caseSource(text, processingOption);
    },
  },
  {
    title: "开始时间",
    dataIndex: "processStartTime",
    key: "processStartTime",
    align: "center",
    customRender: (text, record, index) => {
      return filterDate(text);
    },
  },
  {
    title: "结束时间",
    dataIndex: "processEndTime",
    key: "processEndTime",
    align: "center",
    customRender: (text, record, index) => {
      return filterDate(text);
    },
  },
  {
    title: "备注",
    dataIndex: "remark",
    key: "remark",
    align: "center",
  },
];

const MaterialColumns = [
  {
    title: "工序序号",
    dataIndex: "workStageNum",
    key: "workStageNum",
    // customRender: (text, record, index) => `${record.level + "级"}`,
    align: "center",
  },
  {
    title: "工序名称",
    dataIndex: "workStageName",
    key: "workStageName",
    // customRender: (text, record, index) => `${record.level + "级"}`,
    align: "center",
  },
  {
    title: "设备编号",
    dataIndex: "equipmentNumber",
    key: "equipmentNumber",
    align: "center",
  },
  {
    title: "投料框码",
    dataIndex: "materialCode",
    key: "materialCode",
    // customRender: (text, record, index) => `${record.level + "级"}`,
    align: "center",
  },
  {
    title: "投料数量",
    dataIndex: "castCount",
    key: "castCount",
    align: "center",
  },
  {
    title: "投料时间",
    dataIndex: "materialTime",
    key: "materialTime",
    align: "center",
  },
  {
    title: "操作者",
    dataIndex: "operator",
    key: "operator",
    align: "center",
  },
  {
    title: "备注",
    dataIndex: "materialRemark",
    key: "materialRemark",
    align: "center",
  },
];

const OutputColumns = [
  {
    title: "工序序号",
    dataIndex: "workStageNum",
    key: "workStageNum",
    // customRender: (text, record, index) => `${record.level + "级"}`,
    align: "center",
  },
  {
    title: "工序名称",
    dataIndex: "workStageName",
    key: "workStageName",
    // customRender: (text, record, index) => `${record.level + "级"}`,
    align: "center",
  },
  {
    title: "设备编号",
    dataIndex: "equipmentNumber",
    key: "equipmentNumber",
    align: "center",
  },
  {
    title: "产出框码",
    dataIndex: "outputCode",
    key: "outputCode",
    // customRender: (text, record, index) => `${record.level + "级"}`,
    align: "center",
  },
  {
    title: "产出数量",
    dataIndex: "outputCount",
    key: "outputCount",
    align: "center",
  },
  {
    title: "坏件数量",
    dataIndex: "badCount",
    key: "badCount",
    align: "center",
  },
  {
    title: "投料时间",
    dataIndex: "outputTime",
    key: "outputTime",
    align: "center",
  },

  {
    title: "不合格原因",
    dataIndex: "badCountReason",
    key: "badCountReason",
    align: "center",
    customRender: (text, record, index) => {
      return caseBadReson(text);
    },
  },
  {
    title: "操作者",
    dataIndex: "operator",
    key: "operator",
    align: "center",
  },
  {
    title: "备注",
    dataIndex: "outputRemark",
    key: "outputRemark",
    align: "center",
  },
];

export { columns, orderColumns, MaterialColumns, OutputColumns };
